// 创建并使用路由
const createRouter = require('router')
const router = createRouter()
const queryString = require('querystring')

const { template } = require('./template');
const { Student } = require('../model/Student');

router.get('/index', (req, res) => {
    const html = template('index', {})
    res.end(html);
});
router.post('/index', (req, res) => {
    let data = '';
    req.on('data', chunk => {
        data += chunk;
    })
    req.on('end', async () => {
        data = queryString.parse(data);
        // console.log(data);
        await Student.create(data);
        res.writeHead(301,{ Location: '/list' });
        res.end();
    })
});
router.get('/list', async (req, res) => {
    const studentArr = await Student.find();
    const html = template('list', { studentArr })
    res.end(html);
});

module.exports = { router }